/*
 * Copyright � 2008 Nokia Corporation.
 */


#include "GraphicEngine.h"

#include <akniconutils.h>
#include <fbs.h>
#include <sudoku.mbg>
#include <sudoku_svg.mbg>

_LIT(KBitmapFile, "\\resource\\apps\\sudoku.mbm");
_LIT(KSvgFile, "\\resource\\apps\\sudoku_svg.mbm");

CGraphicEngine::CGraphicEngine ( )
    {
    // No implementation required
    }

CGraphicEngine::~CGraphicEngine ( )
    {
    iBitmapArray.ResetAndDestroy();
    iSVGBitmapArray.ResetAndDestroy();
    }

CGraphicEngine* CGraphicEngine::NewLC ( )
    {
    CGraphicEngine* self = new (ELeave)CGraphicEngine();
    CleanupStack::PushL (self );
    self->ConstructL ( );
    return self;
    }

CGraphicEngine* CGraphicEngine::NewL ( )
    {
    CGraphicEngine* self=CGraphicEngine::NewLC ( );
    CleanupStack::Pop ( ); // self;
    return self;
    }

void CGraphicEngine::ConstructL ( )
    {
    LoadBitmapsL();
    LoadSVGBitmapsL();
    }

void CGraphicEngine::LoadBitmapsL()
    {
    // Loads .bmp type of images into array, NOTE: Do not load mask!
    for (TInt i=EMbmSudokuBackground ; i<EMbmSudokuLastElement ; i+=2 )
        {
        CFbsBitmap* bitmap = AknIconUtils::CreateIconL(KBitmapFile, i);
        iBitmapArray.Append(bitmap);
        bitmap = NULL;
        }
    // NOTE: does not set size of the bitmaps, that will be done later on SetBitmapSize()
    }

void CGraphicEngine::LoadSVGBitmapsL()
    {
    // Loads .svg type of vector images into array (image and mask)
    for (TInt i=EMbmSudoku_svgDarkgriditem ; i<EMbmSudoku_svgLastElement ; i+=2)
        {
        CFbsBitmap* bitmap = NULL;
        CFbsBitmap* mask = NULL;
        AknIconUtils::CreateIconL(bitmap, mask, KSvgFile, i, i+1);
        iSVGBitmapArray.Append(bitmap);
        iSVGBitmapArray.Append(mask);
        }
    // NOTE: does not set size of the bitmaps, that will be done later on SetSVGBitmapSize()
    }

void CGraphicEngine::SetBitmapSize(TInt aIndex, TSize aSize)
    {
    // Update .bmp bitmap image and mask size
    TInt index = (aIndex - EMbmSudokuBackground)/2;
    CFbsBitmap* bitmap = iBitmapArray[index];

    AknIconUtils::SetSize(bitmap, aSize, EAspectRatioNotPreserved);
    }

void CGraphicEngine::SetSVGBitmapSize(TInt aIndex, TSize aSize)
    {
    // Update .svg bitmap image and mask size
    TInt index = aIndex - EMbmSudoku_svgDarkgriditem;
    CFbsBitmap* bitmap = iSVGBitmapArray[index];
    CFbsBitmap* mask = iSVGBitmapArray[index+1];

    AknIconUtils::SetSize(bitmap, aSize, EAspectRatioNotPreserved);
    AknIconUtils::SetSize(mask, aSize, EAspectRatioNotPreserved);
    }


CFbsBitmap* CGraphicEngine::Bitmap(TInt aIndex)
    {
    TInt index = (aIndex - EMbmSudokuBackground)/2;
    return iBitmapArray[index];
    }

CFbsBitmap* CGraphicEngine::BitmapSVG(TInt aIndex)
    {
    TInt index = aIndex - EMbmSudoku_svgDarkgriditem;
    return iSVGBitmapArray[index];
    }

